package com.pr.benchmark;

import com.pr.util.IdGenerator;
import lombok.experimental.UtilityClass;

import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @description:
 * @projectName:spring-skill
 * @see:com.pr.benchmark
 * @author:LXC
 * @createTime:2024/6/5 16:29
 * @version:1.0
 */
@UtilityClass
public class IdWork {
    @Deprecated
    public static String nextByCalendarAndRandom(String merchantNo) {
        Calendar now = Calendar.getInstance();
        long random1 = Math.round(Math.random() * 9);
        long random2 = Math.round(Math.random() * 9);
        String timestamp = (now.get(Calendar.YEAR) + "").substring(2)
                + (now.get(Calendar.MONTH) + 1)
                + now.get(Calendar.DAY_OF_MONTH)
                + now.get(Calendar.HOUR_OF_DAY)
                + now.get(Calendar.MINUTE)
                + now.get(Calendar.SECOND)
                + now.get(Calendar.MILLISECOND);
        return merchantNo + timestamp + random1 + random2;
    }

    @Deprecated
    public static String nextByLocalDateTimeAndRandom(String merchantNo) {
        LocalDateTime now = LocalDateTime.now();
        long random1 = Math.round(Math.random() * 9);
        long random2 = Math.round(Math.random() * 9);
        String timestamp = (now.getYear() + "").substring(2)
                + now.getMonthValue()
                + now.getDayOfMonth()
                + now.getHour()
                + now.getMinute()
                + now.getSecond()
                + (now.getNano() / 1000000);
        return merchantNo + timestamp + random1 + random2;
    }

    @Deprecated
    public static String nextByCalendarAndAtomicInteger(String merchantNo) {
        Calendar now = Calendar.getInstance();
        String timestamp = (now.get(Calendar.YEAR) + "").substring(2)
                + (now.get(Calendar.MONTH) + 1)
                + now.get(Calendar.DAY_OF_MONTH)
                + now.get(Calendar.HOUR_OF_DAY)
                + now.get(Calendar.MINUTE)
                + now.get(Calendar.SECOND)
                + now.get(Calendar.MILLISECOND);
        return merchantNo + timestamp + getSeqNo();
    }

    @Deprecated
    public static String nextByLocalDateTimeAndAtomicInteger(String merchantNo) {
        LocalDateTime now = LocalDateTime.now();
        String timestamp = (now.getYear() + "").substring(2)
                + now.getMonthValue()
                + now.getDayOfMonth()
                + now.getHour()
                + now.getMinute()
                + now.getSecond()
                + (now.getNano() / 1000000);
        return merchantNo + timestamp + getSeqNo();
    }

    public static String nextBySnowflake(String merchantNo) {
        return merchantNo + IdGenerator.next();
    }

    private static AtomicInteger seqNo = new AtomicInteger(1);

    private static String getSeqNo() {
        int curSeqNo = seqNo.getAndIncrement();
        if (curSeqNo > 99) { // 重置,也可以取模
            seqNo = new AtomicInteger(1);
        }
        if (curSeqNo < 10) {
            return "0" + curSeqNo;
        }
        return curSeqNo + "";
    }

    public static void main(String[] args) {
        String next1 = IdWork.nextByCalendarAndRandom("900087");
        System.out.println(next1);
        String next2 = IdWork.nextByLocalDateTimeAndRandom("900087");
        System.out.println(next2);
        String next3 = IdWork.nextByCalendarAndAtomicInteger("900087");
        System.out.println(next3);
        String next4 = IdWork.nextByLocalDateTimeAndAtomicInteger("900087");
        System.out.println(next4);
        String next5 = IdWork.nextBySnowflake("900087");
        System.out.println(next5);
    }
}
